///Iditarod Trail
//Amelia Bauer, Elise Eiden, Christine Gerardi, Taryn Green
//CSE 20212 2012
/*Main Gui window. Contains buttons for all other activities*/

#include "MainWindow.h"
#include "Player.h"
#include "AreYouSureWindow.h"
#include "NameWindow.h"
#include "store.h"
#include "DeluxeDog.h"
#include "Dog.h"
#include "textfield.h"
#include "GenericWindow.h"
#include "Map.h"
#include <iostream>
#include <stdio.h>
#include <QtGui>
#include <QLineEdit> 
#include <QPushButton>
#include <QApplication>
#include <QGridLayout>
#include <QString>
#include <QLabel>
#include <QFrame>
#include <QImage>
#include <QPixmap>
#include <QMainWindow>
#include <QPalette>
#include <QHBoxLayout>
#include <QFont>
#include <string>
#include <cstring>
#include <QPushButton>
#include <QTimer>
#include <vector>

using namespace std;

MainWindow::MainWindow() {

	index = 0;

	timer = new QTimer(this);
	us = new Player();
	
	//Initialize store array
	store[0] = 0;
	store[1] = 15;
	store[2] = 40;
	store[3] = 125;
	store[4] = 140;
	store[5] = 180;

	srand(time(NULL));

	/* establish X and Y dimensions */
	int xSize = 700;
	int ySize = 512;

	
	/*initialize window*/
	QString mainTitle("IDITAROD TRAIL");
	setWindowTitle(mainTitle);
	resize(xSize,ySize);
	
	
	QPalette p = palette();
	QPixmap pixmap1("03-free-wood-textures.jpg" );
	QSize size(xSize,ySize);
	QPixmap pixmap(pixmap1.scaled(size));
	p.setBrush(QPalette::Background,  pixmap);
	setPalette(p);
	
	QWidget *widget = new QWidget();
	/*Make Layouts*/
	
	big_layout = new QVBoxLayout(widget);
	top_layout = new QHBoxLayout();
	miles_layout = new QGridLayout();
	stats_layout = new QGridLayout();
	control_layout = new QGridLayout();
	bottom_layout = new QHBoxLayout();
	
	/*top layout elements*/
	/*make MAP button*/
	
	QPushButton *mapButton = new QPushButton(QString("MAP"),this);
	
	/*logo*/
	QLabel *imageLabel = new QLabel;
	QPixmap pixmap2("Iditarod Logo3x3.jpg");
	imageLabel->setPixmap(pixmap2);
	//imageLabel->setMask(pixmap2.mask());
	
	
	/*miles to go labels*/
	QLabel *next_miles = new QLabel(QString("   Miles to next city:"));
	QLabel *last_miles = new QLabel(QString(" Total miles travelled:"));
	/*set colors*/
	next_miles->setStyleSheet("color:indianred;" "background-color: peachpuff;");
	next_miles->setFixedSize(170,30);
	last_miles->setStyleSheet("color:indianred;" "background-color: peachpuff;");
	last_miles->setFixedSize(170,30);
	
	
	/*miles to go text boxes*/
	next_miles_edit = new QLineEdit(QString(QString::number(store[index]-us->getLocation())), this);
	next_miles_edit->setFixedSize(50,30);
	next_miles->setFont(QFont("Times New Roman", 14, QFont::Bold, true));
	last_miles->setFont(QFont("Times New Roman", 14, QFont::Bold, true));
	last_miles_edit = new QLineEdit(QString("0"), this);
	last_miles_edit->setFixedSize(50,30);


	/*days traveled label*/
	QLabel *days_traveled = new QLabel(QString("      Days travelled:"));
	days_traveled->setFont(QFont("Times New Roman", 14, QFont::Bold, true));
	days_traveled->setStyleSheet("color:indianred;" "background-color: peachpuff;");
	days_traveled->setFixedSize(170,30);
	/*days traveled text box*/
	days_traveled_edit = new QLineEdit(QString(QString::number(us->getDays())));
	days_traveled_edit->setFixedSize(50,30);

	/*bottom layout elements*/
	/*stat labels*/
	QLabel *stat1 = new QLabel(QString("  Number of dogs:"));
	QLabel *stat2 = new QLabel(QString("Number of sick dogs:"));
	QLabel *stat3 = new QLabel(QString("  Food remaining:"));
	
	//set fonts for stat labels
	stat1->setFont(QFont("Times New Roman", 14, QFont::Bold, true));
	stat1-> setStyleSheet("color: indianred;""background-color: peachpuff;");
	stat2->setFont(QFont("Times New Roman", 14, QFont::Bold, true));
	stat2-> setStyleSheet("color: indianred;""background-color: peachpuff;");
	stat3->setFont(QFont("Times New Roman", 14, QFont::Bold, true));
	stat3-> setStyleSheet("color: indianred;""background-color: peachpuff;");	

	/*stat line edits*/
	stat1_edit = new QLineEdit(QString(QString::number(us->getNumDog())));
	stat2_edit = new QLineEdit(QString(QString::number(us->getNumSick())));
	stat3_edit = new QLineEdit(QString(QString::number(us->getFood())));

	rest = new QPushButton(QString("REST"),this);
	med = new QPushButton(QString("USE MEDICINE"),this);
	hunt = new QPushButton(QString("HUNT"),this);
	exit = new QPushButton(QString("EXIT"),this);

	/*animation stand-in*/
	background = new QMovie("IditarodGif.gif");
	box = new QLabel(this);
	box->setFrameStyle(QFrame::Box | QFrame::Raised);
	box->setAlignment(Qt::AlignCenter | Qt::AlignRight);
	box->setMovie(background);
	background->start();
	background->stop();	//Stop immediately so that it adds it, but doesn't go
	
		
	//box->setPixmap(pixmap2);	
	
	/*add things to layouts*/
	/*top layout*/
	top_layout->addWidget(mapButton);
	top_layout->addWidget(imageLabel);
	miles_layout->addWidget(next_miles,1,1);
	miles_layout->addWidget(last_miles,2,1);
	miles_layout->addWidget(next_miles_edit,1,2);
	miles_layout->addWidget(last_miles_edit,2,2);
	miles_layout->addWidget(days_traveled,3,1);
	miles_layout->addWidget(days_traveled_edit,3,2);
	top_layout->addLayout(miles_layout);
	
	/*bottom layout*/
	stats_layout->addWidget(stat1,1,1);
	stats_layout->addWidget(stat2,2,1);
	stats_layout->addWidget(stat3,3,1);
	stats_layout->addWidget(stat1_edit,1,2);
	stats_layout->addWidget(stat2_edit,2,2);
	stats_layout->addWidget(stat3_edit,3,2);

	control_layout->addWidget(rest,1,1);
	control_layout->addWidget(med,1,2);
	control_layout->addWidget(hunt,2,1);
	control_layout->addWidget(exit,2,2);
	
	bottom_layout->addLayout(stats_layout);
	bottom_layout->addLayout(control_layout);
	
	/*big layout*/
	big_layout->addLayout(top_layout);
	big_layout->addWidget(box);
	big_layout->addLayout(bottom_layout);		
	
	//setLayout(big_layout);
	setCentralWidget(widget);
	
	
	QLabel *racer = new QLabel(box);
	racer->setFixedSize(710,400);
	racer->setAlignment(Qt::AlignCenter | Qt::AlignRight);
	QPixmap racerpixmap ("racerMan2.png");
	racer->setPixmap(racerpixmap);
	
 //DOGS
       QLabel *dog1 = new QLabel(box);
       dog1->setFixedSize(625,400);
       dog1->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       QLabel *dog2 = new QLabel(box);
       dog2->setFixedSize(580,400);
       dog2->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       QLabel *dog3 = new QLabel(box);
       dog3->setFixedSize(535,400);
       dog3->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       QLabel *dog4 = new QLabel(box);
       dog4->setFixedSize(490,400);
       dog4->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       QLabel *dog5 = new QLabel(box);
       dog5->setFixedSize(445,400);
       dog5->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       QLabel *dog6 = new QLabel(box);
       dog6->setFixedSize(400,400);
       dog6->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       QLabel *dog7 = new QLabel(box);
       dog7->setFixedSize(355,400);
       dog7->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       QLabel *dog8 = new QLabel(box);
       dog8->setFixedSize(310,400);
       dog8->setAlignment(Qt::AlignCenter | Qt::AlignRight);

       //dog running gif
       dogrun = new QMovie("dogrun3.gif");

       dogLabels.push_back(dog1);
       dogLabels.push_back(dog2);
       dogLabels.push_back(dog3);
       dogLabels.push_back(dog4);
       dogLabels.push_back(dog5);
       dogLabels.push_back(dog6);
       dogLabels.push_back(dog7);
       dogLabels.push_back(dog8);

       //racer
  int numAlive = us->getNumAlive();
  for (int i = 0; i < numAlive; i++)  {
             dogLabels[i]->setMovie(dogrun);
               dogrun->start();
		dogrun->stop();
       }


	//Connect slots and buttons (and timer)
	connect(timer, SIGNAL(timeout()), this, SLOT(playGame()));
	connect(mapButton, SIGNAL(clicked()), this, SLOT(mapClicked()));
	connect(exit, SIGNAL(clicked()), this, SLOT(openNewWindow()));	
	connect(rest, SIGNAL(clicked()), this, SLOT(restFunction()));
	connect(hunt, SIGNAL(clicked()), this, SLOT(huntFunction()));
	connect(med, SIGNAL(clicked()), this, SLOT(medFunction()));	
	restingWindow = new GenericWindow(2, "", QString(""), timer, NULL);	

	//Display start up menu, only once
	setup = new MyTextField(us, timer, NULL);
	setup->show();

}
//Displays the map after stopping animation & timer on main window
void MainWindow:: mapClicked()
{
	timer->stop();
	dogrun->stop();
	background->stop();
	mapp = new Map(us->getLocation(), timer, NULL);
	mapp->show();
}
//Actually plays the game, happens every 3 seconds 
void MainWindow:: playGame()
{
	int event = 0;	//To be used to check if anything happened, if not can check for an event such as Palin, Blizzard, or Yeti 
	//First check if game is done
	int game = us->gameDone();
	if(game == 1)			//Wrong way
	{
		event = 1;
		timer->stop();
		dogrun->stop();
		background->stop();
		GenericWindow *wrongWay = new GenericWindow(4, "", QString(""), timer, NULL);
		wrongWay->show();
	}
	else if(game == 2)		//Win
	{
		event = 1;
		timer->stop();
		dogrun->stop();
		background->stop();
		GenericWindow *winning = new GenericWindow(5, us->getName(), QString(""), timer, NULL);
		winning->show();
	}
	else if(game == 3)		//Lose, all dogs dead
	{
		event = 1;
		timer->stop();
		dogrun->stop();
		background->stop();
		GenericWindow *loser = new GenericWindow(6, us->getName(), QString(""), timer, NULL);
		loser->show();
	}
	else
	{
		updateWindow();	//Display check (i.e. add dogs & start gifs)
		int eventStore = checkStore(); 	//Check if a store is near 
		if(eventStore)
			event = 1;
		//Update text edit displays 
		next_miles_edit->setText(QString::number(store[index]-us->getLocation()));
		last_miles_edit->setText(QString::number(us->getLocation()));
		stat1_edit->setText(QString::number(us->getNumAlive()));
		stat2_edit->setText(QString::number(us->getNumSick()));
		stat3_edit->setText(QString::number(us->getFood()));
		days_traveled_edit->setText(QString::number(us->getDays()));
		us->play();	//Play function in player 

		int eventDog = dogSick();	//Check if a dog is sick or dies 
		if(eventDog)
			event = 1;
		//Check events if no others have occured
		if(event == 0)
		{
			//2% Sarah Palin (pass 7)
			//7% Blizzard (pass 9)
			//5% Yeti (pass 8)
			int randNum = rand()%100;
			if(randNum < 2)	
			{
				timer->stop();
				dogrun->stop();
				background->stop();
				GenericWindow *palin = new GenericWindow(7,"", QString("   Sarah Palin shot your dog!"), timer, NULL);
				palin->exec();	//First displays Palin
				string dogName = us->palinAttack();
				timer->stop();
				dogrun->stop();
				background->stop();
				GenericWindow *ripPalin = new GenericWindow(1,dogName, QString(""), timer, NULL);
				ripPalin->show();	//Then displays the grave 
			}
			else if(randNum < 9) //8
			{
				timer->stop();
				dogrun->stop();
				background->stop();
				GenericWindow *blizz = new GenericWindow(8,"", QString("   Blizzard! Lose 3 days."), timer, NULL);
				blizz->show();
				us->restForDays(2);	//Rests for 3 days (2 and then time advances once) 
			}
			else if(randNum < 14)	//9
			{
				timer->stop();
				dogrun->stop();
				background->stop();
				GenericWindow *yetiEvent = new GenericWindow(9,"", QString(" Yeti attacks and eats your food!"), timer, NULL);
				yetiEvent->show();
				us->yetiAttack();	//Calls function to adjust for Yeti's attack 
			}
		}
	}
}
//Opens exit pop up after stopping game & gifs 
void MainWindow:: openNewWindow()
{
	timer->stop();
	dogrun->stop();
	background->stop();
	exitWindow = new AreYouSureWindow(timer, NULL);
	exitWindow->show();
}
//Opens generic window with rest message then rests for proper number of days after stopping game & animation
void MainWindow:: restFunction()
{
	timer->stop();
	dogrun->stop();
	background->stop();
	restingWindow->exec();
	daysToRest = restingWindow->getRestDays();
	us->restForDays(daysToRest);
}
//For debugging/initial play 
void MainWindow:: setClicked()
{
	setup = new MyTextField(us, NULL);
	setup->show();
}
//Decides if a dog is sick or dying
int MainWindow:: dogSick()
{
	int sick = us->checkIfSick();
	if(sick < 8)
	{
		diedWindowFcn(sick);	//Calls function to display grave
		return 1;
	}
	return 0;

}
//Displays grave window after stopping game & animation 
void MainWindow:: diedWindowFcn(int index)
{
	timer->stop();
	dogrun->stop();
	background->stop();
	string deadDogStr = us->getDogName(index);
	rip = new GenericWindow(1,deadDogStr, QString(""), timer, NULL);
	rip->show();
}
//Uses medicine or displays error window after stopping game & animation 
void MainWindow:: medFunction()
{
	int medError =	us->useMedicine();
	if(medError)
	{
		timer->stop();
		background->stop();
		dogrun->stop();
		GenericWindow *medWindow = new GenericWindow(10, "", QString(""), timer, NULL);
		medWindow->show();
	}
}
//Displays hunt window after stopping game & animation 
void MainWindow:: huntFunction()
{
	timer->stop();
	dogrun->stop();
	background->stop();
	string temp = us->huntPlayerNames();	//Receives name of deluxe dogs
	int tempInt = us->huntPlayerFood();	//Receives amount of food obtained 
	QString fullDisplay(" You have no dogs that can hunt. ");
	if(temp != "")
	{
		QString start(temp.c_str());
		QString middle(" brought back ");
		QString middleEnd(QString::number(tempInt));
		QString end(" pounds of food. ");
		QString fullDisplay = start.append(middle).append(middleEnd).append(end);	//Makes full display 
		GenericWindow *huntW = new GenericWindow(3, "  ", fullDisplay, timer, NULL);	
		huntW->show();
	}
	else
	{
		GenericWindow *huntFail = new GenericWindow(3,"", QString(" You have no dogs that can hunt. "), timer, NULL);
		huntFail->show();
	}
}
//Checks if a store is within distance then shows it after stopping game & animation 
int MainWindow:: checkStore()
{
	if(index == 5)
		return 0;
	else if(us->getLocation() == store[index])
	{
		switch(index)		//Initializes store at proper time (therefore making sure the player is the current player)
		{
			case 0:
				storeArr[0] = new Store(us, "Lance's", "generalstore1.jpg", timer, NULL);
				break;
			case 1:
				storeArr[1] = new Store(us, "Levi Johnston's", "generalstore6.jpg", timer, NULL);
				break;
			case 2:
				storeArr[2] = new Store(us, "Libby Riddles's", "generalstore2.jpg", timer, NULL);
				break;
			case 3:
				storeArr[3] = new Store(us, "Nikolai's", "generalstore3.jpg", timer, NULL);
				break;
			case 4:
				storeArr[4] = new Store(us, "Tornauq's", "generalstore4.jpg", timer, NULL);
				break;
		}
		timer->stop();
		dogrun->stop();
		background->stop();
		storeArr[index]->show(); //Display next store		
		index ++;
		return 1;
	}
	else if((us->getLocation() < store[index]) && ((us->getLocation() + us->getSpeed()) > store[index]))
	{
		switch(index)		//Initializes store at proper time (therefore making sure the player is the current player)
		{
			case 0:
				storeArr[0] = new Store(us, "Lance's", "generalstore1.jpg", timer, NULL);
				break;
			case 1:
				storeArr[1] = new Store(us, "Levi Johnston's", "generalstore6.jpg", timer, NULL);
				break;
			case 2:
				storeArr[2] = new Store(us, "Libby Riddles's", "generalstore2.jpg", timer, NULL);
				break;
			case 3:
				storeArr[3] = new Store(us, "Nikolai's", "generalstore3.jpg", timer, NULL);
				break;
			case 4:
				storeArr[4] = new Store(us, "Tornauq's", "generalstore4.jpg", timer, NULL);
				break;
		}
		timer->stop();
		dogrun->stop();
		background->stop();
		storeArr[index]->show(); //Display next store		
		index++;
		return 1;
	}
	return 0;
}
//Updates window animation
void MainWindow::updateWindow(){
	QLabel *racer = new QLabel(box);
	racer->setFixedSize(710,400);
	racer->setAlignment(Qt::AlignCenter | Qt::AlignRight);


	background->start();
       for (int i = 0; i < us->getNumAlive(); i++) {
               dogLabels[i]->setMovie(dogrun);
               dogrun->start();
       }
	for(int j = us->getNumAlive(); j<us->getNumDog(); j++)
		dogLabels[j]->hide();
}

